<template>
	<view class="mainbgc mainpadding">
		<view class="neirong">
			<view class="ffffff radius mainpadding" v-for="(item,index) in list" :key="item.sku.id">
				<view class="flexright" @click="delcar(item,index)">
					<view class="cha"></view>
				</view>
				<view class="flexbetween margin_top">
					<view class="" style="width: 8%;" @click="changeitem(item)">
						<!-- 未勾选 -->
						<view class="yigouxuan margin_right3" v-show="item.sku.id!=currentid"></view>
						<!-- 已勾选 -->
						<view class="weigouxuan margin_right2" v-show="item.sku.id==currentid"></view>
					</view>
					<view class="flextops" style="width: 91%;" @click="jumpdetail(item.goods_id)">
						<view class="shilitu">
							<image :src="item.image" mode=""></image>
						</view>
						<view class="flexcolumn1" style="width:63%;">
							<view class="ershil lianghang">{{item.title}}</view>
							<view class="huieshs margin_top2">{{item.sku.difference}}</view>
							<view class="flexbetween margin_top2" style="width: 100%;">
								<view class="qianshu">¥{{item.sum}}</view>
								<view class="jiajian">
									<view class="jianhao" @click.stop="reduceite(item)">
										<view class="line"></view>
									</view>
									<view class="xhtext">{{item.number}}</view>
									<view class="jiahao" @click.stop="addite(item)"></view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="query" @click="submit()">确认</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				list: [],
				currentid: "",
				checkitem: {}
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			jumpdetail(id){
				uni.navigateTo({
					url:"/pages/shopping/shangpinxq?id="+id
				})
			},
			delcar(item, index) {
				let data = [{goods_id: item.goods_id,
							sku_id:item.sku.id}]
				httpRequest.modal('提示', '请再次确认', true, (res) => {
					if (res) {
						httpRequest.request('/api/cart/delCart', 'POST', {
							data
						}).then(res => {
							httpRequest.toast(res.msg)
							if(res.code==1){
								this.list.splice(index,1)
							}
						})
					}
				})
			},
			submit() {
				let data = {
					type: "cart", //detail=商品详情,cart=购物车
					data: [{
						goods_id: this.checkitem.goods_id,
						number: this.checkitem.number,
						sku_id: this.checkitem.sku.id
					}]
				}
				uni.navigateTo({
					url: '/pages/shopping/querendd?msg=' + encodeURIComponent(JSON.stringify(data))
				})
			},
			init() {
				httpRequest.request('/api/cart/cartList', 'GET', {}, false, false, true).then(res => {
					res.data.forEach(item => {
						item.sku.difference = item.sku.difference.join("*")
					})
					this.list = res.data
				})
			},
			changeitem(item) {
				this.currentid = item.sku.id
				this.checkitem = item
			},
			// 数量减
			reduceite(item) {
				let number = Number(item.number) - 1
				if (number == 0) {
					httpRequest.toast("不能再减了哦！")
					return false
				}
				httpRequest.request('/api/cart/clCart', 'POST', {
					number,
					goods_id: item.goods_id,
					sku_id: item.sku.id
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						item.number = Number(item.number) - 1
					}
				})
			},
			// 数量加
			addite(item) {
				let number = Number(item.number) + 1
				httpRequest.request('/api/cart/clCart', 'POST', {
					number,
					goods_id: item.goods_id,
					sku_id: item.sku.id
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						item.number = Number(item.number) + 1
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.neirong {
		padding-bottom: 244rpx;
	}

	.huieshs {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.query {
		background-color: #3298fe;
		padding: 20rpx 320rpx 20rpx 320rpx;
		position: fixed;
		bottom: 54rpx;
		border-radius: 69rpx 69rpx 69rpx 69rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;

	}

	.jiahao {
		background-size: 100% 100%;
		width: 21rpx;
		height: 19rpx;
		position: absolute;
		top: 14rpx;
		right: 12rpx;
		background-image: url('@/static/images/system/jia.png');
	}

	.jianhao {
		width: 22rpx;
		position: absolute;
		left: 13rpx;
		// height: 3.5rpx;
		top: 20rpx;
		height: 20rpx;

		.line {
			height: 3.5rpx;
			width: 22rpx;
			background-color: #999999;
		}
	}

	.xhtext {
		font-size: 31rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: RGBA(34, 141, 255, 1);
	}

	.jiajian {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 164rpx;
		height: 46rpx;
		position: relative;
		border-radius: 20rpx;
		background-size: 100% 100%;
		background-image: url('@/static/images/system/jiajian.png');
	}

	.qianshu {
		font-size: 30rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: RGBA(253, 70, 62, 1);
	}

	.shilitu {
		width: 196rpx;
		height: 188rpx;
		border-radius: 14rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 14rpx;
		}
	}

	.ershib {
		font-size: 28rpx;
		font-family: PingFang SC-Heavy, PingFang SC;
		font-weight: 800;
		color: #000000;
	}

	.yhxiank {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden; //溢出内容隐藏
		text-overflow: ellipsis; //文本溢出部分用省略号表示
		display: -webkit-box; //特别显示模式
		-webkit-line-clamp: 2; //行数
		line-clamp: 2;
		-webkit-box-orient: vertical; //盒子中内容竖直排列
	}

	.flexcolumn1 {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		flex-direction: column;
		height: 200rpx;
	}
</style>
